﻿package Core 
{
	import com.greensock.plugins.TintPlugin;
	import com.greensock.plugins.TweenPlugin;	
	import com.greensock.TweenLite;
	import flash.text.TextFormat;
	import Fonts.CalibriEmbed;
	
	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import flash.utils.getDefinitionByName;	
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class IconButton extends Sprite
	{		
		protected var FIcon:Bitmap;
		protected var FText:TextField;
		protected var FFormat:TextFormat;
		protected var FFilter:GlowFilter;		
		protected var FCover:MovieClip;
		protected var FIcons:Dictionary;
		protected var FTip:Tooltip;
		protected var FContainer:Sprite
		protected var FCurrentIconName:Class;
		
		public function IconButton() 
		{
			useHandCursor = true;
			buttonMode = true;
			FCover = getChildAt(0) as MovieClip;
			FCover.alpha = 0;			
			FContainer = new Sprite();
			FContainer.mouseEnabled = false;
			FContainer.mouseChildren = false;
			addChild(FContainer);
			FFilter = new GlowFilter(0, 0, 10, 10, 0.5, 3);
			TweenPlugin.activate([TintPlugin]);				
			FIcons = new Dictionary();
			FTip = new Tooltip();
			addChild(FTip);
			FTip.filters = [new DropShadowFilter(2, 45, 0, 0.5, 10, 10, 1, 2)];
			FTip.MinY = 15;
			addEventListener(MouseEvent.MOUSE_OVER, OnMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, OnMouseOut);
			FText = new TextField();
			FText.selectable = FText.mouseEnabled = false;
			var vFont:CalibriEmbed = new CalibriEmbed();
			FFormat = new TextFormat();
			FFormat.font = vFont.fontName;
			FFormat.size = 14;
			FText.defaultTextFormat = FFormat;
			addChild(FText);
			Caption = 'Надпись';			
		}
		
		protected function OnMouseOver(e:MouseEvent):void 
		{
			if (CanViewTip)
			{
				FTip.visible = true;
				TweenLite.to(FFilter, 0.5, { alpha: 1, onUpdate:ApplyGlow } );
				if(FIcon)
					TweenLite.to(FContainer, 0.5, { tint:0 } );
			}
			
		}
		
		protected function OnMouseOut(e:MouseEvent):void 
		{
			if (CanViewTip)
			{
				FTip.visible = false;
				TweenLite.to(FFilter, 0.5, { alpha: 0, onUpdate:ApplyGlow } );			
				if(FIcon)
					TweenLite.to(FContainer, 0.5, { tint:0x434343 } );
			}
		}
		
		protected function ApplyGlow():void		
		{
			FContainer.filters = [FFilter];
		}
		
		protected function get CanViewTip():Boolean
		{
			return mouseEnabled;
		}
		
		public function set Caption(value:String):void 
		{
			FText.text = value;
			FText.width = FText.textWidth + 4;
			FText.height = FText.textHeight + 4;			
			FText.x = FCover.width;
			FText.y = (FCover.height - FText.height) / 2;
		}
		
		public function set Icon(AName:Class):void
		{
			if (FIcon)
			{			
				FContainer.removeChild(FIcon);				
			}			
			FIcon = FIcons[AName];			
			FContainer.addChild(FIcon);
			FCurrentIconName = AName;
		}
		
		public function get Icon():Class
		{
			return FCurrentIconName;
		}
		
		public function AddIcon(AName:Class):Bitmap
		{
			var vResult:Bitmap;			
			vResult = new Bitmap(new AName());
			vResult.x = (FCover.width - vResult.width) / 2;
			vResult.y = (FCover.height - vResult.height) / 2;
			vResult.smoothing = true;
			vResult.filters = [FFilter];
			FIcons[AName] = vResult;
			return vResult;
		}
		
		public function set Enabled(AEnabled:Boolean):void 
		{			
			mouseEnabled = AEnabled;
			mouseChildren = AEnabled;
			FCover.mouseEnabled = AEnabled;
			useHandCursor = AEnabled;
			buttonMode = AEnabled;
			FTip.visible = false;
			if (!AEnabled)
			{
				alpha = 0.4;
				TweenLite.to(FContainer, 0, {tint:0x434343});
			}
			else
			{
				alpha = 1;
			}
		}
		
		public function get Enabled():Boolean
		{
			return mouseEnabled;
		}
		
		public function set Text(AText:String):void
		{
			FTip.Text = AText;
		}
		
		public function get Width():Number
		{
			return FCover.width;
		}
		
		public function get FullWidth():Number
		{
			return FText.width + FText.x;
		}
	}
}